package com.test50_JVM;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * 弱引用，WeakReference
 * 只要出现GC操作，都会被回收
 */
public class Demo06_WeakReference {

    public static void main(String[] args) {
        // test1();
        test2();
    }

    public static void test1() {
        String str = new String("沐言科技：www.yootk.com");
        WeakReference<String> ref = new WeakReference<>(str);   // 弱引用
        str = null;
        System.gc();
        System.out.println(ref.get());
    }

    public static void test2() {
        String key = new String("yootk");
        String value = new String("沐言科技：www.yootk.com");
        Map<String, String> map = new WeakHashMap<>();  // 弱引用集合
        map.put(key, value);
        key = null;
        System.out.println("【GC处理前】" + map);
        System.gc();
        System.out.println("【GC处理后】" + map);
    }
}
